<html>
	<head></head>
	<body style="background-color:#3f51b5">
<?php
require('common.inc.php');
function get_user_info($uid){
    $sql = 'SELECT coin_addr FROM ld_user_info where id = '.$uid;
    require_once "./ethereum/dbc.php";
    $db = new Dbc($GLOBALARR['dbhost'], $GLOBALARR['dbuser'],$GLOBALARR['dbpwd'], 'utf8', $GLOBALARR['dbname']);
    return $db->select_one($sql);
}
if($_POST["action"]=="save"){
	$user=checkstr(trim($_POST["user"]));
	$pwd=checkstr(trim($_POST["pwd"]));
	$yzm=checkstr(trim($_POST["yzm"]));
	//$ga=checkstr(trim($_POST['index_ga']));
	if(empty($user)) msg_b("请输入账号");
	if(empty($pwd)) msg_b("请输入密码");
	if(empty($yzm)) msg_b("请输入验证码");
    $code=strtolower($_SESSION["code"]);
	if($code!=$yzm) msg_l("您输入的验证码不正确","login.php?uname=".$user);

	//检测用户
	$row=$member->getuserinfo($user,1);
	if(empty($row)) msg_l("您输入的账户不正确","login.php");
	/*
	$param['rgpwd1'] = $row['rgpwd1'];
	$param['pwd'] = $pwd;
	$param['pwd2'] = getpwd($pwd);
	die(json_encode($param));*/
	if($row['rgpwd1']!=getpwd($pwd)) msg_l("您输入的密码不正确","login.php?uname=".$user);
	if($row['is_passed']==0)  msg_l("您的账号未激活，请找您的上级激活","login.php");
	if($row['id']!=8501){
		//检测系统是否开启

		if($GLOBALARR['syopen']&&$_SERVER['HTTP_HOST']!=$GLOBALARR['syopens']){
			echo "<script>window.location.href='index.php';</script>";
			exit;
		}

		if($GLOBALARR['sytime'][1]>$GLOBALARR['sytime'][0]){
			if(strtotime(date('H:i'))<strtotime($GLOBALARR['sytime'][0])||strtotime(date('H:i'))>strtotime($GLOBALARR['sytime'][1])){
				msg_l("系统开放访问时间".$GLOBALARR['sytime'][0]."到".$GLOBALARR['sytime'][1],"login.php");
			}
		}else{
			if(strtotime(date('H:i'))>strtotime($GLOBALARR['sytime'][1])&&strtotime(date('H:i'))<strtotime($GLOBALARR['sytime'][0])){
				msg_l("系统开放访问时间".$GLOBALARR['sytime'][0]."到".$GLOBALARR['sytime'][1],"login.php");
			}
		}
	}

	if($row["is_freezed"]&&$row["id"]!=8501) msg_l("您的帐号已经被冻结，无法登录","login.php");

/* 	if($row['ga']){
		if(!$ga){
			msg_b('请输入双重验证码');
		}
		$ga_n = new GoogleAuthenticator();
		$arr = explode('|', $row['ga']);
		// 存储的信息为谷歌密钥
		$secret = $arr[0];
		// 存储的登录状态为1需要验证，0不需要验证
		$ga_is_login = $arr[1];
		// 判断是否需要验证
		if($ga_is_login){
			// 判断登录有无验证码
			$aa = $ga_n->verifyCode($secret, $ga, 1);
			if (!$aa){
				msg_b('双重身份验证码错误！');
			}
		}
	} */
	$loginip=getrealip();
	$host=$_SERVER['HTTP_HOST'];
	
	// if($host!='119.28.59.47'){
	// 	echo "<script>location.href='login.php';</script>";
	// }
	//保存session
	$_SESSION["userid"]=$row['id'];
	$_SESSION["savetime"]=time();
	$_SESSION["logintime"]=time();
	//file_put_contents(SITEROOT."/login/".$row['id'].".txt",time());
	//记录登录日志
	$db->sitedbexec("update ld_user_info set login_time=".time().",loginip='".$loginip."' where id = ".$row['id']);
	$db->sitedbexec("insert into ld_login_log(uname,ll_time,ll_status,loginip) values('".$user."',".time().",0,'".$loginip."')");

    require_once "./ethereum/dbc.php";
    $dbc = new Dbc($GLOBALARR['dbhost'], $GLOBALARR['dbuser'],$GLOBALARR['dbpwd'], 'utf8', $GLOBALARR['dbname']);
    $sql = $dbc->select_one('SELECT vic_address FROM ld_user_account where id = '.$_SESSION["userid"]);
    if(!$sql){
        require_once('./ethereum/Ethereum.php');
        $CoinClient = new EthClient('172.22.108.36',18554);
        $addres_pass = $CoinClient->personal_newAccount(md5(md5($_SESSION["userid"])));
        $dbc->update(['vic_address'=>$addres_pass,'vic_pass'=>md5(md5($_SESSION["userid"]))], 'ld_user_account', 'id = '.$_SESSION["userid"]);
    }
	//go登录
	echo "<script>location.href='home.php?lsact=index';</script>";
}
echo "<script>location.href='login.php';</script>";
?>
	</body>
</html>
